<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="计划名称" prop="taskName">
                <el-input style="width: 11vw" v-model="queryParams.taskName" placeholder="请输入计划名称" clearable/>
            </el-form-item>
            <el-form-item label="巡检班组" prop="inspectionTeams">
                <el-select v-model="queryParams.inspectionTeams" placeholder="请选择巡检班组" clearable>
                    <el-option v-for="team in teamName" :value="team.teamId" :label="team.teamName"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="planList">
            <el-table-column label="任务名称" align="center" prop="taskName">
                <template #default="{row}">
                    <el-button type="primary" link @click="detailedInformation(row)">{{ row.taskName }}</el-button>
                </template>
            </el-table-column>
            <el-table-column label="巡检类型" align="center" prop="frequencyType">
                <template #default="{row}">
                    <dict-tag :options="inspection_type" :value="row.frequencyType"/>
                </template>
            </el-table-column>
            <el-table-column label="巡检班组" align="center" prop="teamName"/>
            <el-table-column label="创建人" align="center" prop="createBy"/>
            <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
            <el-table-column label="开始日期" align="center" prop="startDate" width="160"/>
            <el-table-column label="结束日期" align="center" prop="endDate" width="160"/>
            <el-table-column label="更新人" align="center" prop="updateBy"/>
            <el-table-column label="更新时间" align="center" prop="updateTime" width="160"/>
            <el-table-column label="备注" align="center" prop="notes"/>
            <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width"
                             v-hasPermi="['ar_check:pending_approval:approve']">
                <template #default="{row}">
                    <el-button link type="primary" icon="Select" @click="apply(row, true)">
                        通过
                    </el-button>
                    <el-button link type="primary" icon="Close" @click="apply(row, false)">
                        驳回
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize"
                    @pagination="getList"/>

        <info ref="info"/>
    </div>
</template>

<script setup>
import { ApprovalListPlan, approvePlan } from '@/api/ar_check/plan'
import DictTag from '@/components/DictTag/index.vue'
import Info from '@/views/ar_check/plan/info.vue'
import { getTeamListAll } from '@/api/ar_check/team.js'

const { proxy } = getCurrentInstance()

defineOptions({
    name: 'Audit'
})

const planList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const teamName = ref([])
const { inspection_type } = proxy.useDict('inspection_type')
const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        frequencyType: null,
        frequencyValue: null,
        areas: null,
        auditTime: null,
        startDate: null,
        endDate: null,
        inspectionTeams: null,
        inspectionItems: null,
        status: null,
        notes: null
    }
})

const { queryParams } = toRefs(data)

/** 查询巡检计划列表 */
async function getList() {
    loading.value = true
    const { rows, total: resTotal } = await ApprovalListPlan(queryParams.value)
    planList.value = rows
    total.value = resTotal
    loading.value = false
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef')
    handleQuery()
}

/*查询未分配的班组列表*/
async function getTeamName() {
    const { data } = await getTeamListAll()
    teamName.value = data
}

/** 打开详细列表 */
function detailedInformation(row) {
    proxy.$refs.info.open(row)
}

/*发起申请*/
async function apply(row, flag) {
    const params = { ...row }
    params.flag = flag
    const { data } = await approvePlan(params)
    if (data) {
        proxy.$modal.msgSuccess('已通过')
        await getList()
    }
}

getList()
getTeamName()
</script>
